package com.alibaba.cloud.ai.example.helloworld.tools;

import cn.hutool.json.JSONUtil;
import com.alibaba.cloud.ai.example.flight.model.Flight;
import com.alibaba.cloud.ai.example.flight.model.command.FlightCommand;
import com.alibaba.cloud.ai.example.flight.model.query.FlightQuery;
import com.alibaba.cloud.ai.example.flight.service.FlightService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.ai.model.function.FunctionCallingOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

/**
 * 航班对应的工具TOOLS
 */
@Configuration
public class FlightCallMethod {
    @Autowired
    private FlightService flightService;

    @GetMapping("/list")
    public List<Flight> getFlights() {
        return flightService.queryFilghtList();
    }

    @GetMapping("/detail1")
    public Flight getFlight1(@RequestParam("name") String name, @RequestParam("orderId") Integer orderId) {
        return flightService.queryFlight(name, orderId);
    }

    @PostMapping("/detail2")
    public Flight getFlight2(@RequestBody FlightQuery query) {
        return flightService.queryFlight(query);
    }

    @PostMapping("/add")
    public String addFlight(@RequestBody FlightCommand command) {
        flightService.addFlight(command);
        return "success";
    }

    /**
     * 1-查询航班详情
     */
    @Bean("queryFlightDetail")
    @Description("查询航班详情")
    public Function<FlightQuery, String> queryFlightDetail() {
        return queryFlightDetail -> {
            // apply 调用查询方法
            Flight flight = flightService.queryFlight(queryFlightDetail);
            if (Objects.nonNull(flight)) {
                return JSONUtil.toJsonStr(flight);
            }
            return Strings.EMPTY;
        };
    }

    /**
     * 2-查询航班列表
     */
    @Bean("queryFlightList")
    @Description("查询航班列表")
    public Function<String, String> queryFlightList() {
        return queryFlightList -> {
            // apply 调用查询方法
            List<Flight> flights = flightService.queryFilghtList();
            if (!CollectionUtils.isEmpty(flights)) {
                return JSONUtil.toJsonStr(flights);
            }
            return Strings.EMPTY;
        };
    }


    @Bean
    public List<FunctionCallingOptions> functionCallDefinitions(Function<String, String> queryFlightList) {
        HashSet<String> functionSet = new HashSet<>();
        functionSet.add("queryFlightDetail");

        return Collections.singletonList( FunctionCallingOptions.builder()
                        .functions(functionSet)
                        .build()
        );
    }
}
